<?php
/**
 * User: Lijun
 * Date: 2017/6/9.
 * Time: 10:22.
 * Comment:友情链接
 */

namespace Admin\Controller;
use Think\Controller;

class FriendLinkController extends Controller{
    public function friendlink(){
        $count =  M('friendlink') -> count();
        $page = new \Think\Page($count,10);
        $show = $page -> show();
        $friendlink = M("friendlink") ->order("id") -> limit($page -> firstRow.','.$page -> listRows) -> select();
        $this -> assign('page',$show);
        $this -> assign('friendlink',$friendlink);
        $this -> display();
    }

    public function addFriendLink(){
        $this -> display();
    }

    public function doAddFriendLink(){
        if(IS_POST){
            $upload = new \Think\Upload();
            $upload -> maxSize = 3145728;
            $upload -> exts = array("jpg","jpeg","png","gif");
            $upload -> autoSub = false;
            $upload -> rootPath = "./Public/";
            $upload -> savePath = "uploads/";
            if($image = $upload -> upload()){
                $_POST['image'] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
            }else{
                $this -> error("error");
            }
            if(M('friendlink') -> add($_POST)){
                $this -> success('添加成功',U('friendlink'));
            }else{
                $this -> error('添加失败');
            }
        }
    }

    public function modFriendLink(){
        if(IS_GET){
            $id = htmlspecialchars($_GET['id']);
            $friendlink = M("friendlink") -> select($id);
        }
        $this -> assign('friendlink',$friendlink[0]);
        $this -> display();
    }

    public function doModFriendLink(){
        if(IS_POST){
            $id = $_POST['id'];
            unset($_POST['id']);
            $oldImage = $_POST['oldImage'];
            unset($_POST['oldImage']);
            $upload = new \Think\Upload();
            $upload -> maxSize = 3145728;
            $upload -> exts = array("jpg","jpeg","png","gif");
            $upload -> autoSub = false;
            $upload -> rootPath = "./Public/";
            $upload -> savePath = "uploads/";
            if($image = $upload -> upload()){
                $_POST['image'] = "/Public/".$image["image"]["savepath"].$image["image"]["savename"];
            }else{
                $this -> error("error");
            }
            if(M('friendlink') -> where("id = {$id}") -> save($_POST)){
                unlink("{$_SERVER['DOCUMENT_ROOT']}/Public/image/{$oldImage}");
                $this -> success('修改成功',U('friendlink'));
            }else{
                $this -> error('修改失败');
            }
        }
    }

    public function delFriendLink(){
        if(session('isLogin') != 1){
            header("Location:".U('Login/index'));
        }
        if(IS_POST){
            $id = $_POST['id'];
            $where = "id in (".implode(',', $id).")";
            if(M("friendlink") -> where($where) -> delete()){
                $this -> success("删除成功",U('friendlink'));
            }else{
                $this -error("删除失败");
            }
        }
        if(IS_GET){
            $id = htmlspecialchars(I('id'));
            if(M('friendlink') -> where("id = {$id}") -> delete()){
                $this -> success('删除成功',U('friendlink'));
            }else{
                $this -> error('删除失败');
            }
        }


    }
}